package com.wjr.braveheart.utils;

import android.app.Application;
import android.content.Context;
import android.widget.Toast;

/**
 * Toast工具
 */
public class ToastUtils {
    private static Application app;
    private static Toast mToast;

    /**
     * private控制不应该被实例化
     */

    private ToastUtils() {
        throw new UnsupportedOperationException("不能被实例化");
    }

    public static void init(Application app) {
        ToastUtils.app = app;
    }

    public static void show(String msg) {
        if (app == null) return;
        show(app, msg);
    }

    public static void showLong(String msg) {
        if (app == null) return;
        showLong(app, msg);
    }

    public static void show(Context context, String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        }
        mToast.show();
    }


    public static void showLong(Context context, String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        } else {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        }
        mToast.show();
    }

    public static void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }
}
